package com.xwj.back.service.impl.user;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xwj.back.mapper.UserMapper;
import com.xwj.back.pojo.User;
import com.xwj.back.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public JSONObject getAllUser(String role,Integer page) {
        IPage<User> userPage = new Page<>(page,6);
        JSONObject resp = new JSONObject();
        if(!"ROLE_ADMIN".equals(role)){
            resp.put("message","failed");
        }
        List<User> users = userMapper.selectPage(userPage, null).getRecords();
        for (User user : users) {
            user.setPassword("");
        }
        Long total = userMapper.selectCount(null);
        resp.put("message","success");
        resp.put("users",users);
        resp.put("total",total);
        return resp;
    }

    @Override
    public List<User> getUsers() {
        return userMapper.selectList(null);
    }

    @Override
    public Map<String, String> updateUserStatus(Map<String, String> data) {
        String role = data.get("role");
        Map<String, String> map = new HashMap<>();
        if(!"ROLE_ADMIN".equals(role)){
            map.put("message","failed");
            return map;
        }
        Integer id = Integer.valueOf(data.get("id"));
        boolean status = Boolean.parseBoolean(data.get("status"));
        userMapper.updateStatus(id,status);
        map.put("message","success");
        return map;
    }

    @Override
    public Map<String, String> addUser(Map<String, String> data) {
        String operateRole = data.get("operateRole");
        Map<String, String> map = new HashMap<>();
        if(!"ROLE_ADMIN".equals(operateRole)){
            map.put("message","failed");
            return map;
        }
        String username = data.get("username");
        //用户名不能重复
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        if(userMapper.selectOne(wrapper) != null){
            map.put("message","repeat");
            return map;
        }
        String phone = data.get("phone");
        String photo = data.get("photo");
        String role = data.get("role");
        String password = passwordEncoder.encode(phone);
        User user = new User(null,username,password,photo,phone,role,true,null);
        userMapper.insert(user);
        map.put("message","success");
        return map;
    }

    @Override
    public Map<String, String> updateUser(Map<String, String> data) {
        String role = data.get("role");
        Map<String, String> map = new HashMap<>();
        if(!"ROLE_ADMIN".equals(role)){
            map.put("message","failed");
            return map;
        }
        Integer id = Integer.valueOf(data.get("id"));
        String username = data.get("username");
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);//用户名不能重复
        User user = userMapper.selectById(id);
        if(!user.getUsername().equals(username) && userMapper.selectList(queryWrapper).size() >= 1 ){ //如果有重复就直接返回
            map.put("message","repeat");
            return map;
        }
        String photo = data.get("photo");
        String phone = data.get("phone");
        String userRole = data.get("userRole");
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id)
                .set("username",username)
                .set("photo",photo)
                .set("phone",phone)
                .set("role",userRole);
        userMapper.update(null,updateWrapper);
        map.put("message","success");
        return map;
    }

    @Override
    public Map<String, String> deleteUser(Map<String, String> data) {
        String role = data.get("role");
        String id = data.get("id");
        Map<String, String> map = new HashMap<>();
        if(!"ROLE_ADMIN".equals(role)){
            map.put("message","failed");
            return map;
        }
        userMapper.deleteById(id);
        map.put("message","success");
        return map;
    }

    @Override
    public JSONObject getUsersByRole(String role,String operateRole) {
        JSONObject resp = new JSONObject();
        if(!"ROLE_ADMIN".equals(operateRole)){
            resp.put("message","failed");
            return resp;
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role",role);
        List<User> users = userMapper.selectList(queryWrapper);
        resp.put("message","success");
        resp.put("users",users);
        return resp;
    }

    @Override
    public JSONObject getUsersByName(String role, String username) {
        JSONObject resp = new JSONObject();
        if(!"ROLE_ADMIN".equals(role)){
            resp.put("message","failed");
            return resp;
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("username",username);
        List<User> users = userMapper.selectList(queryWrapper);
        resp.put("message","success");
        resp.put("users",users);
        return resp;
    }

    @Override
    public String bulkDelete(Map<String, String> data) {
        String role = data.get("role");
        if(!"ROLE_ADMIN".equals(role)){
            return "error";
        }
        List<Integer> select = JSONObject.parseObject(data.get("select"), new TypeReference<List<Integer>>(){});
        userMapper.deleteBatchIds(select);
        return "success";
    }
}
